package com.daervin.svc.manager;

import com.daervin.svc.common.dto.AddSettingDTO;
import com.daervin.svc.common.dto.CategoryTagTypeDTO;
import com.daervin.svc.common.dto.CategoryTagValDTO;
import com.daervin.svc.common.dto.UserDTO;
import com.daervin.svc.common.exception.CommonException;
import com.daervin.svc.common.exception.ErrorCode;
import com.daervin.svc.common.utils.JsonUtil;
import com.daervin.svc.common.utils.Md5Util;
import com.daervin.svc.dal.WhSettingMapper;
import com.daervin.svc.dal.WhUserMapper;
import com.daervin.svc.dal.model.WhSettingDO;
import com.daervin.svc.dal.model.WhUserDO;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by daervin on 2023/8/21
 */
@Component
public class SettingManager {
    @Resource
    private WhSettingMapper whSettingMapper;
    @Resource
    private WhUserMapper whUserMapper;

    public boolean addOrUpdate(AddSettingDTO request) {
        if (request == null || StringUtils.isEmpty(request.getTagSetting())) {
            throw new CommonException(ErrorCode.ERROR_PARAM, "系统配置参数不合法");
        }
        List<CategoryTagValDTO> dtoList = JsonUtil.toObject(request.getTagSetting(), new TypeReference<List<CategoryTagValDTO>>() {
        });
        if (CollectionUtils.isEmpty(dtoList)) {
            throw new CommonException(ErrorCode.ERROR_PARAM, "解析系统配置失败");
        }
        for (CategoryTagValDTO dto : dtoList) {
            if (dto.getType() == null || StringUtils.isEmpty(dto.getType().getVal()) || StringUtils.isEmpty(dto.getType().getDesc())) {
                throw new CommonException(ErrorCode.ERROR_PARAM, "解析系统配置失败-标签类型信息不能为空");
            }
            if (StringUtils.isEmpty(dto.getVal()) || StringUtils.isEmpty(dto.getValDesc())) {
                throw new CommonException(ErrorCode.ERROR_PARAM, "解析系统配置失败-标签值信息不能为空");
            }
        }

        String newExt = JsonUtil.toString(dtoList);
        WhSettingDO record = whSettingMapper.selectOne();
        if (record == null || !newExt.equals(record.getExt())) {
            record = new WhSettingDO();
            record.setExt(newExt);
            whSettingMapper.insertSelective(record);
            return true;
        }
        return true;
    }

    public List<CategoryTagValDTO> getAll() {
        WhSettingDO record = whSettingMapper.selectOne();
        if (record == null || StringUtils.isEmpty(record.getExt())) {
            return Collections.emptyList();
        }
        List<CategoryTagValDTO> dtoList = JsonUtil.toObject(record.getExt(), new TypeReference<List<CategoryTagValDTO>>() {
        });
        return dtoList;
    }

    public Map<String, CategoryTagValDTO> getTagValMap() {
        List<CategoryTagValDTO> list = getAll();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        return list.stream().collect(Collectors.toMap(CategoryTagValDTO::getVal, Function.identity(), (k1, k2) -> k2));
    }

    public Map<String, CategoryTagTypeDTO> getTagTypeMap() {
        List<CategoryTagValDTO> list = getAll();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        Map<String, CategoryTagTypeDTO> typeMap = new HashMap<>();
        list.stream().forEach(it -> {
            typeMap.put(it.getType().getVal(), it.getType());
        });
        return typeMap;
    }

    public CategoryTagValDTO match(String tagType, String tagVal) {
        List<CategoryTagValDTO> list = getAll();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        for (CategoryTagValDTO item : list) {
            if (item.getType().getVal().equalsIgnoreCase(tagType) && item.getVal().equalsIgnoreCase(tagVal)) {
                return item;
            }
        }
        return null;
    }

    public UserDTO matchUser(UserDTO requestUser) {
        if (StringUtils.isEmpty(requestUser.getUsername()) || StringUtils.isEmpty(requestUser.getPassword())) {
            throw new CommonException(ErrorCode.LOGIN_ERROR);
        }
        WhUserDO whUserDO = whUserMapper.selectOne(requestUser.getUsername(), Md5Util.generateMD5(requestUser.getPassword()));
        if (whUserDO == null) {
            throw new CommonException(ErrorCode.LOGIN_ERROR);
        }
        UserDTO userDTO = new UserDTO();
        userDTO.setUsername(whUserDO.getUsername());
        userDTO.setNickname(whUserDO.getNickname());
        return userDTO;
    }
}
